﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace DeployForwardMailboxRules
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();
		}

		/// <summary>
		/// Add User Email Address event.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void addEmail_Click(object sender, RoutedEventArgs e)
		{
			if (String.IsNullOrEmpty(UserEmailAddress))
			{
				return;
			}

			if (IsAlreadyExistsInDataGrid(UserEmailAddress))
			{
				MessageBox.Show("Email Address already exists in Grid.");
				return;
			}

			emailsGrid.Items.Add(UserEmailAddress);
		}

		/// <summary>
		/// Remove Email Address from List event.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void removeEmail_Click(object sender, RoutedEventArgs e)
		{
			if (emailsGrid.SelectedIndex == -1)
			{
				MessageBox.Show("Select email address in grid to remove it.");
				return;
			}

			emailsGrid.Items.RemoveAt(emailsGrid.SelectedIndex);
		}

		/// <summary>
		/// Create Rules event.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void createRules_Click(object sender, RoutedEventArgs e)
		{
			if (!FormDataCheck())
			{
				return;
			}
			Service.ExchangeOnPremise serviceProvider = new Service.ExchangeOnPremise(UserName, Password, null, EWSEndpoint, UseAutoDiscover);

			foreach (string address in emailsGrid.Items)
			{
				try
				{
					serviceProvider.DeployForwardMailboxRule(address, ForwardMailbox);
				}
				catch (Exception ex)
				{
					MessageBoxResult result = MessageBox.Show(string.Format("Exception occured while creating rule for email address: {0}, Error Message: {1}\nWant to continue further processing?", address, ex.Message), "Error", MessageBoxButton.YesNo);
					if (result == MessageBoxResult.No)
					{
						break;
					}
				}
			}
			MessageBox.Show("Operation Completed.");
		}

		/// <summary>
		/// Delete Rules event.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void deleteRules_Click(object sender, RoutedEventArgs e)
		{
			if (!FormDataCheck())
			{
				return;
			}
			Service.ExchangeOnPremise serviceProvider = new Service.ExchangeOnPremise(UserName, Password, null, EWSEndpoint, UseAutoDiscover);

			foreach (string address in emailsGrid.Items)
			{
				try
				{
					serviceProvider.DeleteForwardMailboxRule(address, ForwardMailbox);
				}
				catch (Exception ex)
				{
					MessageBoxResult result = MessageBox.Show(string.Format("Exception occured while creating rule for email address: {0}, Error Message: {1}\nWant to continue further processing?", address, ex.Message), "Error", MessageBoxButton.YesNo);
					if (result == MessageBoxResult.No)
					{
						break;
					}
				}
			}
			MessageBox.Show("Operation Completed.");
		}

		/// <summary>
		/// Not using currently.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void clearGrid_Click(object sender, RoutedEventArgs e)
		{

		}

		/// <summary>
		/// UseAutoDiscover checkbox change event.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void useAutoDiscover_Checked(object sender, RoutedEventArgs e)
		{
			if (UseAutoDiscover)
			{
				ewsEndpoint.IsEnabled = false;
			}
			else
			{
				ewsEndpoint.IsEnabled = true;
			}
		}

		/// <summary>
		/// EmailAddressBox KeyDown event to provide {ENTER} functionality.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void emailAddressBox_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.Key == Key.Enter)
			{
				addEmail_Click(sender, null);
			}
		}

		/// <summary>
		/// Checks whether same email address already exists in list.
		/// </summary>
		/// <param name="emailAddress"></param>
		/// <returns></returns>
		private bool IsAlreadyExistsInDataGrid(string emailAddress)
		{
			foreach (string address in emailsGrid.Items)
			{
				if (string.Equals(emailAddress, address))
				{
					return true;
				}
			}
			return false;
		}

		/// <summary>
		/// Checks whether Form data is in valid state for Create/Delete actions.
		/// </summary>
		/// <returns></returns>
		private bool FormDataCheck()
		{
			if (string.IsNullOrEmpty(UserName))
			{
				MessageBox.Show("Please provide Exchange Admin User Name.");
				return false;
			}
			if (string.IsNullOrEmpty(Password))
			{
				MessageBox.Show("Please provide password.");
				return false;
			}
			if (string.IsNullOrEmpty(EWSEndpoint) && !UseAutoDiscover)
			{
				MessageBox.Show("Either use Auto-discover or provide EWS endpoint address");
				return false;
			}
			if (string.IsNullOrEmpty(ForwardMailbox))
			{
				MessageBox.Show("Please provide Forward mailbox address.");
				return false;
			}
			return true;
		}

		#region Properties
		public string UserName
		{
			get
			{
				if (!string.IsNullOrEmpty(userName.Text))
				{
					return userName.Text.Trim();
				}
				else
				{
					return string.Empty;
				}
			}
		}

		public string Password
		{
			get
			{
				if (!string.IsNullOrEmpty(password.Password))
				{
					return password.Password.Trim();
				}
				else
				{
					return string.Empty;
				}
			}
		}

		public bool UseAutoDiscover
		{
			get
			{
				return useAutoDiscover.IsChecked.Value;
			}
		}

		public string EWSEndpoint
		{
			get
			{
				if (!string.IsNullOrEmpty(ewsEndpoint.Text))
				{
					return ewsEndpoint.Text.Trim();
				}
				else
				{
					return string.Empty;
				}
			}
		}

		public string ForwardMailbox
		{
			get
			{
				if (!string.IsNullOrEmpty(forwardEmailAddress.Text))
				{
					return forwardEmailAddress.Text.Trim();
				}
				else
				{
					return string.Empty;
				}
			}
		}

		public string UserEmailAddress
		{
			get
			{
				if (!string.IsNullOrEmpty(emailAddressBox.Text))
				{
					return emailAddressBox.Text.Trim();
				}
				else
				{
					return string.Empty;
				}
			}
		}
		#endregion
	}
}
